<template>
	<view class="content">
		<view class="top-block2" v-if="isFiles==1&&picState==0">
			<view class="iconfont icon-tishi"><text class="text">您的现场图片提交成功，管理员会在1-3个工作日内审核，请耐心等待。</text></view>
		</view>
		<view class="top-block1" v-if="picState==1">
			<view class="text" v-if="auditRemark"><text class="iconfont icon-tishi"></text>审核已通过：{{auditRemark}}，成功获得积分
			</view>
			<view class="iconfont icon-tishi" v-else><text class="text">您提交的现场图片已通过，成功获得积分。</text></view>
		</view>
		<view class="top-block3" v-if="picState==2">
			<view class="text" v-if="auditRemark"><text class="iconfont icon-tishi"></text>审核未通过：{{auditRemark}}</view>
			<view class="iconfont icon-tishi" v-else><text class="text">您提交的现场图片未通过。</text></view>
		</view>
		<view class="title" v-if="isFiles==0||picState==2">请上传现场图片</view>
		<view class="title" v-if="isFiles==1&&picState!=2">现场图片</view>
		<view class="upload-item">
			<view v-for="(item,index) in picList" :key="item.id" class="image-item">
				<image :src="item" class="photo" mode="aspectFill" @click="previewImg(item)"></image>
				<view @click="closeImg(index)" class="iconfont icon-guanbi"
					v-if="picList.length>0&&isFiles==0||picState==2"></view>
			</view>
			<view class="upload-content" @click="chooseImg" v-if="picList.length<6&&isFiles==0||picState==2">
				<image src="/static/images/shopping/upload.png" class="upload">
				</image>
			</view>
		</view>
		<view class="btn-item" @click="submit" v-if="isFiles==0||picState==2">
			<view class="btn">提交</view>
		</view>
	</view>
</template>

<script>
	import {
		getStorage,
		setStorage
	} from '@/util/auth.js';
	import {
		submitImgApi,
		getImgListApi
	} from '@/api/activity.js'
	export default {
		data() {
			return {
				id: null,
				picList: [],
				isFiles: 0,
				picState: 0,
				auditRemark: ''
			}
		},
		methods: {
			previewImg(current) {
				uni.previewImage({
					current,
					urls: this.picList
				})
			},
			closeImg(index) {
				var that = this;
				uni.showModal({
					title: '提示',
					content: '是否删除当前图片',
					success: function(res) {
						if (res.confirm) {
							that.picList.splice(index, 1);
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			chooseImg(e) {
				let token = getStorage('token');
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						res.tempFilePaths.forEach(item => {
							uni.showLoading({
								title: '上传中',
								mask: true
							})
							const filePath = res.tempFilePaths[0];
							uni.uploadFile({
								url: 'https://www.hxzxczx.cn/api/upload',
								filePath: filePath,
								name: 'file',
								header: {
									'authori-zation': token
								},
								success: (myres) => {
									uni.hideLoading()
									const data = JSON.parse(myres.data);
									if (data.code == 200) {
										this.picList.push(data.data)
									} else {
										uni.hideLoading()
										this.showToast('上传失败');
									}
								}
							})
						})
					}
				})
			},
			submit() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				if (this.picList.length > 0) {
					const files = this.picList.join(',')
					submitImgApi({
						files: files,
						activityId: this.id
					}).then(res => {
						uni.hideLoading()
						uni.showToast({
							title: '提交成功',
							icon: 'success',
							duration: 1000,
							mask: true,
							success: function() {
								setTimeout(function() {
									uni.switchTab({
										url: '/pages/mine/mine'
									})
								}, 1000)
							},
						})
					}).catch(() => {
						uni.hideLoading()
					})
				} else {
					this.showToast('请选择图片')
					return false
				}
			},
			getDetail() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				getImgListApi({
					activityId: this.id
				}).then(res => {
					uni.hideLoading()
					this.auditRemark = res.auditRemark
					if (res.files && res.files.length > 0) {
						res.files = res.files.split(',');
					}
					this.picList = res.files
				}).catch(() => {
					uni.hideLoading()
				})
			},
		},
		onLoad(e) {
			console.log(e)
			this.id = e.id
			this.isFiles = e.state
			if (this.isFiles == 1) {
				this.getDetail()
			}
			this.picState = e.picState
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100%;
		background: #fff;
		min-height: 100vh;
		padding: 30rpx;

		.top-block1 {
			background: #D5EBE8;
			padding: 30rpx;
			border-radius: 20rpx;
			margin-bottom: 30rpx;
			display: flex;
			align-items: center;

			.text {
				font-size: 30rpx;
				color: #0D9C6E;
				display: flex;
			}

			.iconfont {
				color: #0D9C6E;
				font-size: 40rpx;
				display: flex;
				align-items: center;
			}
		}

		.top-block2 {
			background: #CADFFA;
			padding: 30rpx;
			border-radius: 20rpx;
			margin-bottom: 30rpx;
			display: flex;
			flex-direction: row;
			align-items: center;

			.iconfont {
				color: #1A7EF3;
				font-size: 40rpx;
				margin-top: 4rpx;
			}

			.text {
				font-size: 30rpx;
				margin-left: 10rpx;
				line-height: 50rpx;
			}
		}

		.top-block3 {
			background: #F3D3D6;
			padding: 30rpx;
			border-radius: 20rpx;
			margin-bottom: 30rpx;
			display: flex;
			align-items: center;

			.text {
				font-size: 30rpx;
				color: #DF2411;
				display: flex;
			}

			.iconfont {
				color: #DF2411;
				font-size: 40rpx;
				display: flex;
				align-items: center;
			}
		}

		.title {
			margin-bottom: 20rpx;
		}

		.upload-item {
			display: flex;
			flex-direction: row;
			flex-wrap: wrap;


			.image-item {
				width: calc(100% / 2 - 30rpx);
				margin-right: 30rpx;
				margin-bottom: 20rpx;


				.photo {
					width: 100%;
					height: 260rpx;
					border-radius: 10rpx;
				}

				.iconfont {
					margin-top: 10rpx;
					margin-left: -40rpx;
					position: absolute;
					display: inline;
				}
			}

			.image-item:nth-child(2n) {
				margin-right: 0;
			}

			.upload-content {
				width: 330rpx;
				height: 265rpx;
				background: #F9F9F9;
				display: flex;
				align-items: center;
				justify-content: center;

				.upload {
					width: 120rpx;
					height: 120rpx;
				}
			}
		}

		.btn-item {
			width: calc(100% - 60rpx);
			position: fixed;
			bottom: 6%;
			display: flex;
			justify-content: center;

			.btn {
				background: #157DFA;
				color: #fff;
				width: 340rpx;
				height: 80rpx;
				border-radius: 60rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}
</style>